package com.neuedu.oasystem.view;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import com.neuedu.oasystem.entity.Plan;
import com.neuedu.oasystem.entity.PlanAudit;
import com.neuedu.oasystem.service.PlanAuditService;
import com.neuedu.oasystem.service.PlanService;
import com.neuedu.oasystem.service.impl.PlanAuditServiceImpl;
import com.neuedu.oasystem.service.impl.PlanServiceImpl;

public class PlanSystem {
	private Scanner scanner = null;
	private PlanService planService;
	private PlanAuditService planAuditService;
	SimpleDateFormat sFormat = new SimpleDateFormat("dd-MM-yyyy");
	Date date1 = null;
	Date date2 = null;
	boolean running = false;
	public void init() {
		System.out.println("========================================");
		System.out.println("         欢迎使用工作计划管理系统                                ");
		System.out.println("========================================");
		System.out.println("1.工作计划管理");
		System.out.println("2.工作计划审核");
		System.out.println("3.返回上一级");
		System.out.println("========================================");
	}
	public void work() {
		scanner = new Scanner(System.in);
		planService = new PlanServiceImpl();
		boolean running = true;
		
		while (running) {
			init();
			System.out.print("请选择：");
			String choose = scanner.nextLine();
			switch (choose) {
			case "1":
				System.out.println("调用工作计划管理管理模块");
				work1();
				break;
			case "2":
				System.out.println("调用工作计划审核模块");
				work2();
				break;
			case "3":
				System.out.println("您选择了退出系统，谢谢使用!");
				running = false;
				break;
			default:
				break;
			}
		}
	}
	
	public void init1() {
		System.out.println("========================================");
		System.out.println("          欢迎使用工作计划管理系统                             ");
		System.out.println("========================================");
		System.out.println("1.工作计划添加");
		System.out.println("2.工作计划修改");
		System.out.println("3.工作计划删除");
		System.out.println("4.工作计划查询");
		System.out.println("5.返回上层");
		System.out.println("========================================");
	}
	public void work1() {
		scanner = new Scanner(System.in);
		planService = new PlanServiceImpl();
		boolean running = true;
		while (running) {
			init1();
			System.out.print("请选择：");
			String choose = scanner.nextLine();
			switch (choose) {
			case "1":
				System.out.println("进入工作计划添加");
				planAdd();
				break;
			case "2":
				System.out.println("进入工作计划修改");
				planEdit();
				break;
			case "3":
				System.out.println("选定工作计划删除");
				planDelete();
				break;
			case "4":
				System.out.println("查询工作计划");
				planlist();
				break;

			case "5":
				System.out.println("返回上层");
				work();
			default:
				running = false;
				break;
			}
		}
	}
	
	public void init2() {
		System.out.println("========================================");
		System.out.println("         欢迎使用工作计划审核系统                                ");
		System.out.println("========================================");
		System.out.println("1.计划意见添加");
		System.out.println("2.计划意见修改");
		System.out.println("3.查询结果 ");
		System.out.println("4.返回上一级");
		System.out.println("========================================");
	}

	public void work2() {
		scanner = new Scanner(System.in);
		planService = new PlanServiceImpl();
		boolean running = true;
		while (running) {
			init2();
			System.out.print("请选择：");
			String choose = scanner.nextLine();
			switch (choose) {
			case "1":
				System.out.println("进入计划意见添加");
				planAuditAdd();
				break;
			case "2":
				System.out.println("进入计划意见修改");
				planAuditEdit();
				break;
			case "3":
				System.out.println("查询结果");
				planAuditlist();
				break;
			case "4":
				System.out.println("返回上层");
				work();
			default:
				running = false;
				break;
			}
		}
	}
	/**
	 * 工作计划表查询
	 */
	private void planlist() {
		List<Plan> list = planService.findAll();
		System.out.println("计划编号\t计划名称\t会议主题\t制定内容\t计划类型\t公告内容\t开始时间\t结束时间\t执行组长名称\t计划组长编号");
		for(Plan plan:list) {
			System.out.println(plan.getPlanId()+"\t"+plan.getPlanTheme()+"\t"+plan.getMeetingTheme()+"\t"+plan.getDevelopContent()+"\t"+plan.getNoticeContent()+"\t"+plan.getPlanType()+"\t"+plan.getPstart()+"\t"+plan.getPfinish()+"\t"+plan.getPlanLeader()+"\t"+plan.getEmpId());
		}
	}
	/**
	 * 工作计划审核结果查询
	 */
	private void planAuditlist() {
		planAuditService = new PlanAuditServiceImpl();
		List<PlanAudit> list = planAuditService.findAll();
		System.out.println("计划编号\t财务意见\t财务意见结果\t经理意见\t经理意见结果");
		for(PlanAudit planaudit:list) {
			System.out.println(planaudit.getPlanId()+"\t"+planaudit.getFad()+"\t"+planaudit.getFinAuditRes()+"\t"+planaudit.getMad()+"\t"+planaudit.getMgrAuditRes());
		}
	}
	/**
	 * 工作计划删除
	 */
	private void planDelete() {
		PlanService planservice = new PlanServiceImpl();
		System.out.println("请输入要删除的工作计划编号");
		int planId = scanner.nextInt();
		planservice.deleteById(planId);
	}
	/**
	 * 工作计划添加
	 */
	private void planAdd() {
		PlanService planservice = new PlanServiceImpl();


		scanner = new Scanner(System.in);

			System.out.println("请输入计划编号：");
			String planId = scanner.next();
			
			System.out.println("请输入计划名称：");
			String planTheme = scanner.next();
			
			System.out.println("请输入会议主题：");
			String meetingTheme = scanner.next();
			
			System.out.println("请输入制定内容：");
			String developContent = scanner.next();
			
			System.out.println("请输入计划类型：");
			String plantype = scanner.next();
			
			System.out.println("请输入公告内容：");
			String noticeContent = scanner.next();
			
			System.out.println("请输入开始时间（dd-mm-yyyy）：");
			String pStart = scanner.next();
			
			try {
				date1 = sFormat.parse(pStart);
			} catch (ParseException e) {
			System.out.println("格式错误请重新输入：");

			}
			
			
			System.out.println("请输入结束时间（dd-mm-yyyy）：");
			String pFinish = scanner.next();
			
			try {
				date2 = sFormat.parse(pFinish);
			} catch (ParseException e) {
				System.out.println("格式错误请重新输入：");
			}
			
			System.out.println("请输入计划组长名称：");
			String planLeader = scanner.next();
			
			System.out.println("请输入计划组长编号：");
			int empId = scanner.nextInt();

		Plan plan = new Plan(planId,planTheme,meetingTheme,developContent,plantype, noticeContent, date1, date2, planLeader,empId);
		if(planservice.add(plan)) {
			System.out.println("新增成功！");
		}else {
			System.out.println("新增失败！");
		}
	}
	
	/**
	 * 工作计划审核添加
	 */
	private void planAuditAdd() {
		PlanAuditService planAuditService = new PlanAuditServiceImpl();
		scanner = new Scanner(System.in);

			System.out.println("请输入计划编号：");
			String planId = scanner.next();
			
			System.out.println("请输入财务意见：");
			String fad = scanner.next();
			
			System.out.println("请输入财务审核结果（同意/不同意）：");
			String finAuditRes = scanner.next();
			
			System.out.println("请输入经理意见：");
			String mad = scanner.next();
			
			System.out.println("请输入经理审核结果（同意/不同意）：");
			String mgrAuditRes = scanner.next();

		PlanAudit planAudit = new PlanAudit(planId,fad,finAuditRes,mad,mgrAuditRes);
		if(planAuditService.add(planAudit)) {
			System.out.println("新增成功！");
		}else {
			System.out.println("新增失败！");
		}
		}
	
	/**
	 * 计划审核修改	
	 */
	private void planAuditEdit() {
		PlanAuditService planAuditService = new PlanAuditServiceImpl();
		scanner = new Scanner(System.in);

			System.out.println("请输入需要修改的计划编号：");
			String planId = scanner.next();
			
			System.out.println("请输新的入财务意见：");
			String fad = scanner.next();
			
			System.out.println("请输入新的财务审核结果（1同意/0不同意）：");
			String finAuditRes = scanner.next();
			
			System.out.println("请输入新的经理意见：");
			String mad = scanner.next();
			
			System.out.println("请输入新的经理审核结果（1同意/0不同意）：");
			String mgrAuditRes = scanner.next();

		PlanAudit planAudit = new PlanAudit(planId,fad,finAuditRes,mad,mgrAuditRes);
		if(planAuditService.edit(planAudit)) {
			System.out.println("更新成功！");
		}else {
			System.out.println("更新失败！");
		}
		}
	
		/**
		 * 工作计划修改
		 */
	private void planEdit() {
		PlanService planService = new PlanServiceImpl();
		scanner = new Scanner(System.in);

			System.out.println("请输入需要修改的计划编号：");
			String planId = scanner.next();
			
			System.out.println("请输入新的计划名称：");
			String planTheme = scanner.next();
			
			System.out.println("请输入新的会议主题：");
			String meetingTheme = scanner.next();
			
			System.out.println("请输入新的制定内容：");
			String developContent = scanner.next();
			
			System.out.println("请输入新的计划类型：");
			String plantype = scanner.next();
			
			System.out.println("请输入新的公告内容：");
			String noticeContent = scanner.next();
			
			System.out.println("请输入新的开始时间（dd-mm-yyyy）：");
			String pStart = scanner.next();
			
			try {
				date1 = sFormat.parse(pStart);
			} catch (ParseException e) {
			System.out.println("格式错误请重新输入：");
			}
			
			System.out.println("请输入新的结束时间（dd-mm-yyyy）：");
			String pFinish = scanner.next();
			
			try {
				date2 = sFormat.parse(pFinish);
			} catch (ParseException e) {
				System.out.println("格式错误请重新输入：");
			}
			
			System.out.println("请输入新的计划组长名称：");
			String planLeader = scanner.next();
			
			System.out.println("请输入计划组长编号：");
			int empId = scanner.nextInt();

		Plan plan = new Plan(planId,planTheme,meetingTheme,developContent,plantype, noticeContent, date1, date2, planLeader,empId);
		if(planService.edit(plan)) {
			System.out.println("修改成功！");
		}else {
			System.out.println("修改失败！");
		}
		}
	
}